import useConfigStore from '@/store/config'
import ls from '@/lib/ls'

export default {
    // 更新购物车的商品
    async RefreshCart(item) {
        console.log(item);
        
        // 筛选是否相同商品
        const existingItem = this.cartList.find((cartItem) => cartItem.itemCode === item.itemCode && cartItem.flavor === item.flavor);
        // 相同
        if (existingItem) {
            if (item.mathType === 'plus') {
                if (item.type == 2) {
                    existingItem.num += item.num
                } else {
                    if(item.addType == 'size'){ // 规格添加
                        existingItem.num += Number(item.num);
                    }else{
                        existingItem.num += Number(item.saleLimitStep);
                        if(existingItem.num >= Number(item.saleLimitMax)){
                            existingItem.num = Number(item.saleLimitMax)
                        }
                    }
                }
            } else if (item.mathType === 'minus') {
                existingItem.num -= Number(item.saleLimitStep);
                await nextTick()
                if (existingItem.num <= 0 || existingItem.num <  Number(item.saleLimitMin)) {
                    console.log(existingItem.num , Number(item.saleLimitMin));
                    
                    const index = this.cartList.indexOf(existingItem);
                    this.cartList.splice(index, 1);
                }
            }
            // 不同
        } else {
            // 点单步长
            if(item.num <= Number(item.saleLimitStep)){
                item.num = Number(item.saleLimitStep)
            }
            // 最小点单数量
            if(item.num <= Number(item.saleLimitMin)){
                item.num = Number(item.saleLimitMin)
            }
            this.cartList.push(item);
        }
        const configStore = useConfigStore()
        this.UpdateSignList(item, configStore.signList)
        this.UpdateSignList(item, configStore.allSignList)
    },
    // 更新商品列表
    UpdateNumFromProductList(item) {
        const configStore = useConfigStore()
        this.UpdateNumRecursive(configStore.signList, item.itemCode, item.num);
        this.UpdateNumRecursive(configStore.allSignList, item.itemCode, item.num);
    },
    // 更新商品列表的递归函数
    UpdateNumRecursive(list, itemCode, num) {
        for (const v of list) {
            if (v.priceList) {
                const matchedPrice = v.priceList.find((price) => price.itemCode === itemCode);
                if (matchedPrice) {
                    v.num = num;
                    return;
                }
            }
            if (v.children) {
                this.UpdateNumRecursive(v.children, itemCode, num);
            }
        }
    },
    // 添加减少额度
    UpdateSignList(product, list) {
        const { itemCode, mathType, salePrice,num,saleLimitStep,addType,type } = product
        console.log(product);
        for (const group of list) {
            for (const groupItem of group.children) {
                const priceList = groupItem.priceList;
                if (priceList) {
                    const foundPrice = priceList.find(price => price.itemCode === itemCode);
                    if (foundPrice) {
                        const usedValue = Number(group.usedValue);
                        const maxValue = Number(group.maxValue);
                        if (mathType === "plus") {
                            if(addType == 'size' || type == '2'){ // type == '2'是套餐  addType == 'size' 是规格
                                if(group.groupType == '1'){
                                    group.usedValue = (usedValue + num * Number(salePrice)).toFixed(2);
                                }else{
                                    group.usedValue = usedValue + Number(num)
                                }
                            }else{
                                if(group.groupType == '1'){
                                    group.usedValue = (usedValue + Number(salePrice)).toFixed(2);
                                }else{
                                    group.usedValue = usedValue + Number(saleLimitStep)
                                }
                            }
                           
                            
                        } else if (mathType === "minus") {
                            if(group.groupType == '1'){
                                group.usedValue = (usedValue - Number(salePrice)).toFixed(2);
                            }else{
                                group.usedValue = usedValue - saleLimitStep
                            }
                            
                        }
                        break;
                    }
                }
            }
        }
    },
    // 重置购物车
    ClearCart() {
        this.cartList.length = 0
        const configStore = useConfigStore()

        configStore.signList = configStore.toolSignList  // 让赠送份额回到最初（拉取数据时的数据）
        configStore.allSignList = configStore.toolallSignList

        for (const product of configStore.signList) {
          for (const childItem of product.children) {
            childItem.showNum = 0;
            childItem.num = 0;
          }
        }
        ls.remove('CART_LIST')
    },
}